package fr.umlv.ig.model;

import java.awt.Point;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class Station {
	private final String name;
	private final Point coordinate;
	private boolean opened;
	private int waitingTime; 
	private final Map<Station, Station> nextStations = new HashMap<Station, Station>();
	private final Map<Station, Integer> nextWaiting = new HashMap<Station, Integer>();
	
	public Station(String name, int x, int y) {
		if(name == null) {
			throw new NullPointerException("The mame is null");
		}
		this.name =name;
		coordinate = new Point(x, y);
		opened = true;
	}
	
	public String getName() {
		return name;
	}
	

	public Point getCoordinate() {
		return coordinate;
	}
	
	public int getX() {
		return coordinate.x;
	}
	
	public int getY() {
		return coordinate.y;
	}
	
	void setOpened(boolean opened) {
		this.opened = opened;
	}
	
	public boolean isOpened() {
		return this.opened;
	}
	
	void setWaitingTime(int waitingTime) {
		this.waitingTime = waitingTime;
	}

	public int getwaitingTime() {
		return waitingTime;
	}

	public Station getNextStation(Station direction) {
		return nextStations.get(direction);
	}
	
	public Map<Station,Station> getMapNextStations() {
		return Collections.unmodifiableMap(nextStations);
	}
	
	void addNextStation(Station direction, Station nextStation) {
		if(direction==null || nextStation ==null) {
			throw new NullPointerException("One or most stations are null");
		}
		nextStations.put(direction, nextStation);
	}
	
	void setWaitingTime(Station station,int time) {
		if(nextWaiting.containsKey(station)) {
			throw new IllegalArgumentException("The station is not in the Line");
		}
		if(time < 0) {
			throw new IllegalArgumentException("The time is negatif");
		}
		nextWaiting.remove(station);
		nextWaiting.put(station,time);
	}
	
	public int getWaitingTime(Station station) {
		if(nextWaiting.containsKey(station)) {
			throw new IllegalArgumentException("The station is not in the Line");
		}
		return nextWaiting.get(station);
	}
	
	Map<Station,Station> getNextStations() {
		return nextStations;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == this) {
			return true;
		}
		if(!(obj instanceof Station)) {
			return false;
		}
		Station s = (Station) obj;
		return name.equals(s.name) && coordinate.equals(s.coordinate); 
	}
	
	@Override
	public int hashCode() {
		return name.hashCode() + coordinate.hashCode();
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[name : ").append(name).append("\n");
		builder.append("coordinate : (").append(coordinate.x).append(",").
			append(coordinate.y).append(")\n");
		builder.append("open : ").append(opened).append("\n");
		builder.append("waitingTime : ").append(waitingTime).append("\n");
		int i=1;
		for(Station next : nextStations.values()) {
			builder.append("neighbor "+i+" : "+next.name ).append("\n");
			i++;
		}
		builder.append("]");
		return builder.toString();
			   
	}
	
	
}
